package cn.mcch.investigator.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import cn.mcch.investigator.listener.OnCommonClickListener;

/**
 * Created by Administrator on 2016/12/19.
 */
public abstract class BaseDialog extends DialogFragment {

    private View root;
    /**
     * 保存子view的集合
     */
    private SparseArray<View> mViewsArray = new SparseArray<View>();

    /**
     * 获取指定id的view
     *
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T extends View> T findViewById(int id) {
        T view = (T) mViewsArray.get(id);
        if (view == null) {
            view = (T) root.findViewById(id);
            mViewsArray.put(id, view);
        }
        return view;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        root = initView(inflater, container);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
        initEventListener();
        initData();
        return root;
    }

    /**
     * 绑定数据，需要的话重写
     */
    public void initData() {

    }

    /**
     * 绑定事件
     */
    protected abstract void initEventListener();

    /**
     * dialog布局
     *
     * @return
     * @param inflater
     * @param container
     */
    protected abstract View initView(LayoutInflater inflater, ViewGroup container);

    /**
     * 设置view可见性
     *
     * @param id
     * @param visibility
     * @return
     */
    public BaseDialog setVisibility(int id, boolean visibility) {
        findViewById(id).setVisibility(visibility ? View.VISIBLE : View.GONE);
        return this;

    }

    /**
     * @param id
     * @param alpha
     * @return
     */
    public BaseDialog setAlpha(int id, float alpha) {
        findViewById(id).setAlpha(alpha);
        return this;

    }

    /**
     * @param id
     * @param checked
     * @return
     */
    public BaseDialog setChecked(int id, boolean checked) {
        ((CheckBox) findViewById(id)).setChecked(checked);
        return this;

    }

    /**
     * 设置AdapterView的适配器
     *
     * @param adapterViewId
     * @param adapter
     */
    @SuppressWarnings("unchecked")
    public BaseDialog setAdapterViewAdapter(int adapterViewId, BaseAdapter
            adapter) {
        ((AdapterView<BaseAdapter>)
                findViewById(adapterViewId)).setAdapter(adapter);
        return this;
    }

    /**
     * 获取view的文本信息
     *
     * @param id
     * @return
     */
    public String getViewText(int id) {
        return ((TextView) findViewById(id)).getText().toString().trim();
    }

    /**
     * 设置view的文本信息
     *
     * @param id
     * @param content
     * @return
     */
    public BaseDialog setText(int id, CharSequence content) {
        ((TextView) findViewById(id)).setText(content);
        return this;

    }

    /**
     * 设置view的点击事件
     *
     * @param l
     * @param ids
     * @return
     */
    public BaseDialog setOnClickListener(OnCommonClickListener l, int... ids) {
        if (l == null || ids == null) {
            return this;
        }

        for (int id : ids) {
            View view = findViewById(id);
            if (view != null) {
                view.setOnClickListener(l);
            }
        }
        return this;
    }

    /**
     * 设置view的长按点击事件
     *
     * @param l
     * @param ids
     * @return
     */
    public BaseDialog setOnLongClickListener(OnCommonClickListener l, int... ids) {
        if (l == null || ids == null) {
            return this;
        }
        for (int id : ids) {
            View view = findViewById(id);
            if (view != null) {
                view.setOnLongClickListener(l);
            }
        }
        return this;
    }


    /**
     * 设置AdapterView的条目点击事件
     *
     * @param l
     * @param ids
     * @return
     */
    @SuppressWarnings("rawtypes")
    public BaseDialog setOnItemClickListener(OnCommonClickListener l, int... ids) {
        if (l == null || ids == null) {
            return this;
        }
        for (int id : ids) {
            AdapterView adapterView = (AdapterView) findViewById(id);
            if (adapterView != null) {
                adapterView.setOnItemClickListener(l);
            }
        }
        return this;
    }

    /**
     * 设置vie的条目长按点击事件
     *
     * @param l
     * @param ids
     * @return
     */
    @SuppressWarnings("rawtypes")
    public BaseDialog setOnItemLongClickListener(OnCommonClickListener l, int... ids) {
        if (l == null || ids == null) {
            return this;
        }
        for (int id : ids) {
            AdapterView adapterView = (AdapterView) findViewById(id);
            if (adapterView != null)
                adapterView.setOnItemLongClickListener(l);
        }
        return this;
    }


}
